package com.nmgs.service.impl;

import com.nmgs.dto.ScreenDto;
import com.nmgs.entity.SetupTable;
import com.nmgs.mapper.OutPortMapper;
import com.nmgs.service.IOutPortService;
import com.nmgs.util.PubTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class OutPortServiceImpl implements IOutPortService {
    @Autowired
    private OutPortMapper outportmapper;
    @Override
    public ScreenDto getLaneData(Map<String, Object> params) {
        ScreenDto screenDto=new ScreenDto();
        try{
            List<SetupTable> setupTableList = outportmapper.getSetupTableList(params);
            if(!setupTableList.isEmpty()){
                int normal=0;
                int error=0;
                for(SetupTable setupTable:setupTableList){
                    boolean b = PubTools.pingIp(setupTable.getPortip(), 1);
                    if(b){
                        normal++;
                    }else{
                        error++;
                    }
                }
                screenDto.setLaneCount(setupTableList.size());
                screenDto.setLaneErrorCount(error);
                screenDto.setLaneNormalCount(normal);
            }
        }catch (Exception e){
            screenDto.setLaneCount(0);
            screenDto.setLaneErrorCount(0);
            screenDto.setLaneNormalCount(0);
            return screenDto;
        }
        return screenDto;
    }
    @Override
    public ScreenDto getFlowAndMoneyByKH(Map<String, Object> params){
        ScreenDto screenDto=new ScreenDto();
        String dateType = params.get("dateType").toString();
        params.put("DBTYPE",PubTools.getDBType());
        if("DAY".equals(dateType)){
            params.put("startTime",PubTools.getdfYMD());
            params.put("endTime",PubTools.getdfYMD());
        }else if("WEEK".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(6));
            params.put("endTime",PubTools.getDecNumDate(0));
        }else if("MONTH".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(29));
            params.put("endTime",PubTools.getDecNumDate(0));
        }
        List<ScreenDto> flowAndMoneyByKH = outportmapper.getFlowAndMoneyByKH(params);
        if(!flowAndMoneyByKH.isEmpty()){
            screenDto=flowAndMoneyByKH.get(0);
        }
        return screenDto;
    }

    @Override
    public List<ScreenDto> getFlowAndMoneyByClassno(Map<String, Object> params){
        String dateType = params.get("dateType").toString();
        params.put("DBTYPE",PubTools.getDBType());
        if("DAY".equals(dateType)){
            params.put("startTime",PubTools.getdfYMD());
            params.put("endTime",PubTools.getdfYMD());
        }else if("WEEK".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(6));
            params.put("endTime",PubTools.getDecNumDate(0));
        }else if("MONTH".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(29));
            params.put("endTime",PubTools.getDecNumDate(0));
        }
        return outportmapper.getFlowAndMoneyByClassno(params);
    }
    @Override
    public List<ScreenDto> getFlowAndMoneyByExitfee(Map<String, Object> params){
        String dateType = params.get("dateType").toString();
        params.put("DBTYPE",PubTools.getDBType());
        if("DAY".equals(dateType)){
            params.put("startTime",PubTools.getdfYMD());
            params.put("endTime",PubTools.getdfYMD());
        }else if("WEEK".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(6));
            params.put("endTime",PubTools.getDecNumDate(0));
        }else if("MONTH".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(29));
            params.put("endTime",PubTools.getDecNumDate(0));
        }
        return outportmapper.getFlowAndMoneyByExitfee(params);
    }
    @Override
    public ScreenDto getFlowAndMoneyBypayType(Map<String, Object> params){
        ScreenDto screenDto=new ScreenDto();
        String dateType = params.get("dateType").toString();
        params.put("DBTYPE",PubTools.getDBType());
        if("DAY".equals(dateType)){
            params.put("startTime",PubTools.getdfYMD());
            params.put("endTime",PubTools.getdfYMD());
        }else if("WEEK".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(6));
            params.put("endTime",PubTools.getDecNumDate(0));
        }else if("MONTH".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(29));
            params.put("endTime",PubTools.getDecNumDate(0));
        }
        List<ScreenDto> flowAndMoneyByKH = outportmapper.getFlowAndMoneyBypayType(params);
        if(!flowAndMoneyByKH.isEmpty()){
            screenDto=flowAndMoneyByKH.get(0);
        }
        return screenDto;
    }
    @Override
    public List<ScreenDto> getFlowAndMoneyByEnname(Map<String, Object> params){
        String dateType = params.get("dateType").toString();
        List<ScreenDto> flowAndMoneyByEnname=new ArrayList<>();
        params.put("DBTYPE",PubTools.getDBType());
        if("DAY".equals(dateType)){
            params.put("startTime",PubTools.getdfYMD());
            params.put("endTime",PubTools.getdfYMD());
        }else if("WEEK".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(6));
            params.put("endTime",PubTools.getDecNumDate(0));
        }else if("MONTH".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(29));
            params.put("endTime",PubTools.getDecNumDate(0));
        }
        List<ScreenDto> flowAndMoneyByEnname2 = outportmapper.getFlowAndMoneyByEnname(params);

        if(!flowAndMoneyByEnname2.isEmpty()){
            for(int k=0;k<flowAndMoneyByEnname2.size();k++){
                ScreenDto screenDto = flowAndMoneyByEnname2.get(k);
                screenDto.setOrderRank(k+1);
                flowAndMoneyByEnname.add(screenDto);
                if(k >8){
                    break;
                }
                }
            }
        return flowAndMoneyByEnname;
    }
    @Override
    public List<ScreenDto> getFlowAndMoneyByTq(Map<String, Object> params){
        String dateType = params.get("dateType").toString();
        List<ScreenDto> flowAndMoneyByEnname=new ArrayList<>();
        params.put("DBTYPE",PubTools.getDBType());
        if("DAY".equals(dateType)){
            params.put("startTime",PubTools.getdfYMD());
            params.put("endTime",PubTools.getdfYMD());
        }else if("WEEK".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(6));
            params.put("endTime",PubTools.getDecNumDate(0));
        }else if("MONTH".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(29));
            params.put("endTime",PubTools.getDecNumDate(0));
        }
        List<ScreenDto> flowAndMoneyByEnname2 = outportmapper.getFlowAndMoneyByTq(params);
        if(!flowAndMoneyByEnname2.isEmpty()){
            for(int k=0;k<flowAndMoneyByEnname2.size();k++){
                ScreenDto screenDto = flowAndMoneyByEnname2.get(k);
                screenDto.setOrderRank(k+1);
                flowAndMoneyByEnname.add(screenDto);
                if(k >8){
                    break;
                }
                }
            }
        return flowAndMoneyByEnname;
    }
    @Override
    public List<ScreenDto> getFlowByRobot(Map<String, Object> params){
        String dateType = params.get("dateType").toString();
        params.put("DBTYPE",PubTools.getDBType());
        if("DAY".equals(dateType)){
            params.put("startTime",PubTools.getdfYMD());
            params.put("endTime",PubTools.getdfYMD());
        }else if("WEEK".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(6));
            params.put("endTime",PubTools.getDecNumDate(0));
        }else if("MONTH".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(29));
            params.put("endTime",PubTools.getDecNumDate(0));
        }
        List<ScreenDto> flowAndMoneyByEnname2 =null;
        if("DAY".equals(dateType)){
            String[] hours24 = new String[24];
            // 使用循环填充数组
            for(int i = 0; i < 24; i++) {
                //hours24[i] = String.format("%02d", i); // 保持两位数格式
                hours24[i] = i+""; // 保持两位数格式
            }
            flowAndMoneyByEnname2=outportmapper.getFlowDayByRobot(params);
            List<ScreenDto> list=new ArrayList<ScreenDto>();
            for(String str:hours24){
                ScreenDto screenDtoTemp=new ScreenDto();
                BigDecimal totalMoney=new BigDecimal("0");
                BigDecimal totalFlow=new BigDecimal("0");
                if(!flowAndMoneyByEnname2.isEmpty()){
                    for (ScreenDto screenDto : flowAndMoneyByEnname2) {
                        if(str.equals(screenDto.getDmDate())){
                            totalMoney=PubTools.isEmpty(screenDto.getTotalMoney())?new BigDecimal("0"):screenDto.getTotalMoney();
                            totalFlow=PubTools.isEmpty(screenDto.getTotalFlow())?new BigDecimal("0"):screenDto.getTotalFlow();
                            break;
                        }
                    }
                    if(str.length()<2){
                        str="0"+str;
                    }
                    screenDtoTemp.setDmDate(str);
                    screenDtoTemp.setTotalMoney(totalMoney);
                    screenDtoTemp.setTotalFlow(totalFlow);
                    list.add(screenDtoTemp);
                }else{
                    if(str.length()<2){
                        str="0"+str;
                    }
                    screenDtoTemp.setDmDate(str);
                    screenDtoTemp.setTotalMoney(new BigDecimal("0"));
                    screenDtoTemp.setTotalFlow(new BigDecimal("0"));
                    list.add(screenDtoTemp);
                }
            }
            flowAndMoneyByEnname2=list;
        }else{
            flowAndMoneyByEnname2=outportmapper.getFlowMWByRobot(params);
            if(!flowAndMoneyByEnname2.isEmpty()){
                for (ScreenDto screenDto : flowAndMoneyByEnname2) {
                    screenDto.setDmDate(PubTools.getDateMonthDay(screenDto.getClassdate()));
                }
            }
        }
        return flowAndMoneyByEnname2;
    }
    @Override
    public List<ScreenDto> getFlowAndMoneyByClassdate(Map<String, Object> params){
        String dateType = params.get("dateType").toString();
        params.put("DBTYPE",PubTools.getDBType());
        if("DAY".equals(dateType)){
            params.put("startTime",PubTools.getdfYMD());
            params.put("endTime",PubTools.getdfYMD());
        }else if("WEEK".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(6));
            params.put("endTime",PubTools.getDecNumDate(0));
        }else if("MONTH".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(29));
            params.put("endTime",PubTools.getDecNumDate(0));
        }
        List<ScreenDto> flowAndMoneyByEnname2 =null;

        if("DAY".equals(dateType)){
            String[] hours24 = new String[24];
            // 使用循环填充数组
            for(int i = 0; i < 24; i++) {
                //hours24[i] = String.format("%02d", i); // 保持两位数格式
                hours24[i] = i+""; // 保持两位数格式
            }
            flowAndMoneyByEnname2=outportmapper.getFlowAndMoneyDayByClassdate(params);
            List<ScreenDto> list=new ArrayList<ScreenDto>();
            for(String str:hours24){
                ScreenDto screenDtoTemp=new ScreenDto();
                BigDecimal totalMoney=new BigDecimal("0");
                BigDecimal totalFlow=new BigDecimal("0");
                if(!flowAndMoneyByEnname2.isEmpty()){
                    for (ScreenDto screenDto : flowAndMoneyByEnname2) {
                        if(str.equals(screenDto.getDmDate())){
                            totalMoney=PubTools.isEmpty(screenDto.getTotalMoney())?new BigDecimal("0"):screenDto.getTotalMoney();
                            totalFlow=PubTools.isEmpty(screenDto.getTotalFlow())?new BigDecimal("0"):screenDto.getTotalFlow();
                            break;
                        }
                    }
                    if(str.length()<2){
                        str="0"+str;
                    }
                    screenDtoTemp.setDmDate(str);
                    screenDtoTemp.setTotalMoney(totalMoney);
                    screenDtoTemp.setTotalFlow(totalFlow);
                    list.add(screenDtoTemp);
                }else{
                    if(str.length()<2){
                        str="0"+str;
                    }
                    screenDtoTemp.setDmDate(str);
                    screenDtoTemp.setTotalMoney(new BigDecimal("0"));
                    screenDtoTemp.setTotalFlow(new BigDecimal("0"));
                    list.add(screenDtoTemp);
                }
            }
            flowAndMoneyByEnname2=list;
        }else{
            flowAndMoneyByEnname2 = outportmapper.getFlowAndMoneyByClassdate(params);
            if(!flowAndMoneyByEnname2.isEmpty()){
                for (ScreenDto screenDto : flowAndMoneyByEnname2) {
                    screenDto.setDmDate(PubTools.getDateMonthDay(screenDto.getClassdate()));
                }
            }
        }
        return flowAndMoneyByEnname2;
    }
    @Override
    public ScreenDto getEveryRateAll(Map<String, Object> params){
        String dateType = params.get("dateType").toString();
        params.put("DBTYPE",PubTools.getDBType());
        if("DAY".equals(dateType)){
            params.put("startTime",PubTools.getdfYMD());
            params.put("endTime",PubTools.getdfYMD());
        }else if("WEEK".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(6));
            params.put("endTime",PubTools.getDecNumDate(0));
        }else if("MONTH".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(29));
            params.put("endTime",PubTools.getDecNumDate(0));
        }
        ScreenDto screenDto=   new ScreenDto();
        List<ScreenDto> etcRate = outportmapper.getEtcRate(params);
        if(!etcRate.isEmpty()){
            ScreenDto screenDto1 = etcRate.get(0);
            if(screenDto1.getTotalFlow().compareTo(new BigDecimal("0"))==0){
                screenDto.setEtcRate(BigDecimal.ZERO);
            }else {
                BigDecimal i = screenDto1.getTotalFlow().subtract(screenDto1.getErrorCount());
                BigDecimal i1 = screenDto1.getTotalFlow();
                screenDto.setEtcRate((i.divide(i1,4, RoundingMode.DOWN).multiply(new BigDecimal("100"))).setScale(2, RoundingMode.DOWN));
            }
        }
        List<ScreenDto> everyRate = outportmapper.getEveryRate(params);
        if(!everyRate.isEmpty()){
            ScreenDto screenDto1 = everyRate.get(0);
            if(screenDto1.getTotalFlow().compareTo(new BigDecimal("0"))==0){
                screenDto.setRgcxRate(BigDecimal.ZERO);
                screenDto.setRgcpRate(BigDecimal.ZERO);
                screenDto.setZztxRate(BigDecimal.ZERO);
            }else {
                BigDecimal i1 = screenDto1.getTotalFlow();
                BigDecimal zztx = screenDto1.getTotalFlow().subtract(screenDto1.getZztx());
                BigDecimal rgcp = screenDto1.getTotalFlow().subtract(screenDto1.getRgcp());
                BigDecimal rgcx = screenDto1.getTotalFlow().subtract(screenDto1.getRgcx());
                screenDto.setZztxRate((zztx.divide(i1,4, RoundingMode.DOWN).multiply(new BigDecimal("100"))).setScale(2, RoundingMode.DOWN));
                screenDto.setRgcpRate((rgcp.divide(i1,4, RoundingMode.DOWN).multiply(new BigDecimal("100"))).setScale(2, RoundingMode.DOWN));
                screenDto.setRgcxRate((rgcx.divide(i1,4, RoundingMode.DOWN).multiply(new BigDecimal("100"))).setScale(2, RoundingMode.DOWN));
            }
        }
        return screenDto;
    }
}
